const router = require('koa-router')()
const { getTextrueList, getModelImgsList, getCourseList } = require('../model/images')
const { getWorkList, getDetail, getMyWorks, getMyStar, getMyLike, addStar, addLike } = require('../model/works')
const { getMineInfo, getMyFollow, addFollow } = require('../model/users')
const { 
  collectModel,
  cancelCollectModel,
  collectTextrue,
  cancelCollectTextrue,
  updateState,
  getMyLikeList,
  getMyModelCollect,
  getMyTextrueCollect,
  getMyWorkCollect,
  getMyUserCollect
 } = require('../model/collect')
const send = require('koa-send');
const { getTextrueFile, getModelFile } = require('../model/download')
const { searchForModels, searchForTextrues, searchForCourses } = require('../model/search')

router.get('/getModels', async(ctx,next) => {
  const page = ctx.request.query.page

  ctx.body = await getModelImgsList(page);
})

router.get('/getTextures', async(ctx,next) => {
  const page = ctx.request.query.page

  ctx.body = await getTextrueList(page);
})

router.get('/getCourses', async(ctx,next) => {
  const page = ctx.request.query.page

  ctx.body = await getCourseList(page);
})

router.get('/getWorks', async(ctx,next) => {
  const page = ctx.request.query.page

  ctx.body = await getWorkList(page);
})

router.get('/getDetail', async(ctx,next) => {
  const id = ctx.request.query.id

  ctx.body = await getDetail(id);
})

router.get('/getMineInfo', async(ctx,next) => {
  const id = ctx.request.query.id

  ctx.body = await getMineInfo(id);
})

router.get('/getMyWorks', async(ctx,next) => {
  const id = ctx.request.query.id

  ctx.body = await getMyWorks(id);
})

router.post('/collectModel', async(ctx,next) => {
  const modelid = ctx.request.body.modelid;
  const userid = ctx.request.body.userid;
  const create_time = ctx.request.body.create_time;

  await collectModel(modelid,userid,create_time);

  ctx.body = {code:200,data:'ok'};
})

router.post('/cancelCollectModel', async(ctx,next) => {
  const modelid = ctx.request.body.modelid;
  const userid = ctx.request.body.userid;

  await cancelCollectModel(modelid,userid);

  ctx.body = {code:200,data:'ok'};
})

router.post('/collectTextrue', async(ctx,next) => {
  const textrueid = ctx.request.body.textrueid;
  const userid = ctx.request.body.userid;
  const create_time = ctx.request.body.create_time;

  await collectTextrue(textrueid,userid,create_time);

  ctx.body = {code:200,data:'ok'};
})

router.post('/cancelCollectTextrue', async(ctx,next) => {
  const textrueid = ctx.request.body.textrueid;
  const userid = ctx.request.body.userid;

  await cancelCollectTextrue(textrueid,userid);

  ctx.body = {code:200,data:'ok'};
})

router.get('/updateState', async(ctx,next) => {
  const id = ctx.request.query.id;
  const type = ctx.request.query.type;

  ctx.body = await updateState(id,type);
})

router.get('/downloadTextrueFile', async(ctx,next) => {
  const fileName = ctx.request.query.fileName;

  let path = `public/images/textrues/${await getTextrueFile(fileName)}`;
  ctx.attachment(path);
  await send(ctx,path);
})

router.get('/downloadModelFile', async(ctx,next) => {
  const fileName = ctx.request.query.fileName;

  let path = `public/models/${await getModelFile(fileName)}`;
  ctx.attachment(path);
  await send(ctx,path);
})

router.get('/searchForModels', async(ctx,next) => {
  const text = ctx.request.query.text;

  ctx.body = await searchForModels(text);
})

router.get('/searchForTextrues', async(ctx,next) => {
  const text = ctx.request.query.text;

  ctx.body = await searchForTextrues(text);
})

router.get('/searchForCourses', async(ctx,next) => {
  const text = ctx.request.query.text;

  ctx.body = await searchForCourses(text);
})

router.get('/getMyFollow', async(ctx,next) => {
  const userid = ctx.request.query.userid;
  const followid = ctx.request.query.followid;

  ctx.body = await getMyFollow(userid,followid);
})

router.get('/getMyStar', async(ctx,next) => {
  const userid = ctx.request.query.userid;
  const workid = ctx.request.query.workid;

  ctx.body = await getMyStar(userid,workid);
})

router.get('/getMyLike', async(ctx,next) => {
  const userid = ctx.request.query.userid;
  const workid = ctx.request.query.workid;

  ctx.body = await getMyLike(userid,workid);
})

router.post('/followedUser', async(ctx,next) => {
  const followid = ctx.request.body.followid;
  const userid = ctx.request.body.userid;
  const state = ctx.request.body.state;

  ctx.body = await addFollow(followid,userid,state);
})

router.post('/staredWork', async(ctx,next) => {
  const userid = ctx.request.body.userid;
  const workid = ctx.request.body.workid;
  const state = ctx.request.body.state;

  ctx.body = await addStar(userid,workid,state);
})

router.post('/likedWork', async(ctx,next) => {
  const userid = ctx.request.body.userid;
  const workid = ctx.request.body.workid;
  const state = ctx.request.body.state;

  ctx.body = await addLike(userid,workid,state);
})

router.get('/getMyLikeList', async(ctx,next) => {
  const userid = ctx.request.query.userid;

  ctx.body = await getMyLikeList(userid);
})

router.get('/getMyModelCollect', async(ctx,next) => {
  const userid = ctx.request.query.userid;
  const page = ctx.request.query.page;

  ctx.body = await getMyModelCollect(userid,page);
})

router.get('/getMyTextrueCollect', async(ctx,next) => {
  const userid = ctx.request.query.userid;
  const page = ctx.request.query.page;

  ctx.body = await getMyTextrueCollect(userid,page);
})

router.get('/getMyWorkCollect', async(ctx,next) => {
  const userid = ctx.request.query.userid;

  ctx.body = await getMyWorkCollect(userid);
})

router.get('/getMyUserCollect', async(ctx,next) => {
  const userid = ctx.request.query.userid;

  ctx.body = await getMyUserCollect(userid);
})

router.post('/cancelFollowUser', async(ctx,next) => {
  const followid = ctx.request.body.followid;
  const userid = ctx.request.body.userid;

  ctx.body = await addFollow(followid,userid,true);
})

module.exports = router
